package com.fox.hiring.company.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.fox.hiring.common.Constant;
import com.fox.hiring.common.DBObject;
import com.fox.hiring.common.PageModel;
import com.fox.hiring.common.util.DBUtil;
import com.fox.hiring.company.entity.ApplicationEntity;

/**
 * Application Dao
 * 
 * @author fox.
 * @since 2016年3月4日
 */
public class ApplicationDao {

	/**
	 * 职位申请
	 * 
	 * @param positionId
	 * @param resumeId
	 * @return
	 */
	public int add(int positionId, int talentId, int resumeId) {
		int row = 0;
		String sql = "INSERT INTO application VALUES(NULL, ?, ?, ?, ?, NOW(), NULL)";
		try {
			List<Object> paramList = new ArrayList<>();
			paramList.add(positionId);
			paramList.add(talentId);
			paramList.add(resumeId);
			paramList.add(Constant.APPLY_NO_CHECK);
			row = DBUtil.executeUpdate(sql, paramList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return row;
	}

	/**
	 * 判断该职位是否已经申请
	 * 
	 * @param positionId
	 * @param talentId
	 * @return
	 */
	public ApplicationEntity isApply(int positionId, int talentId) {
		ApplicationEntity application = null;
		String sql = "SELECT * FROM application WHERE position_id=? AND talent_id=?";
		DBObject dbObject = null;
		try {
			dbObject = DBUtil.executeQuery(sql, positionId, talentId);
			ResultSet resultSet = dbObject.getResultSet();
			if (resultSet.next()) {
				application = new ApplicationEntity();
				application.setStatus(resultSet.getInt("status"));
			}
		} catch (Exception e) {
			application = null;
			e.printStackTrace();
		} finally {
			DBUtil.close(dbObject);
		}
		return application;
	}

	/**
	 * 查询当前登录者所有申请的职位
	 * 
	 * @param talentId
	 * @return
	 */
	public PageModel<ApplicationEntity> listAllApplyInfo(int talentId,
			int pageNo, int pageSize) {
		List<ApplicationEntity> applicationList = new ArrayList<>();
		// 职位名称 所属企业 申请状态 申请时间
		// position company application
		String sql = "SELECT a.id, p.name AS 'position_name', c.name AS 'company_name', a.status, a.app_time FROM position p, company c, application a WHERE a.position_id=p.id AND p.company_id=c.id AND talent_id=? LIMIT ?, ?";
		DBObject dbObject = null;
		try {
			dbObject = DBUtil.executeQuery(sql, talentId, (pageNo - 1)
					* pageSize, pageSize);
			ResultSet resultSet = dbObject.getResultSet();
			while (resultSet.next()) {
				ApplicationEntity application = new ApplicationEntity();
				application.setId(resultSet.getInt("id"));
				application.setPositionName(resultSet
						.getString("position_name"));
				application.setCompanyName(resultSet.getString("company_name"));
				application.setStatus(resultSet.getInt("status"));
				application.setAppTime(resultSet.getTimestamp("app_time"));
				applicationList.add(application);
			}
		} catch (Exception e) {
			// applicationList = new ArrayList<>();
			applicationList = Collections.emptyList();
			e.printStackTrace();
		} finally {
			DBUtil.close(dbObject);
		}
		return new PageModel<ApplicationEntity>(applicationList,
				count(talentId), pageNo, pageSize);
	}

	/**
	 * 查询当前登录者所有申请职位的总记录数
	 * 
	 * @param talentId
	 * @return
	 */
	private int count(int talentId) {
		int count = 0;
		String sql = "SELECT COUNT(*) AS 'count' FROM position p, company c, application a WHERE a.position_id=p.id AND p.company_id=c.id AND talent_id=?";
		DBObject dbObject = null;
		try {
			dbObject = DBUtil.executeQuery(sql, talentId);
			ResultSet resultSet = dbObject.getResultSet();
			if (resultSet.next()) {
				count = resultSet.getInt("count");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(dbObject);
		}
		return count;
	}

	/**
	 * 删除职位申请信息
	 * 
	 * @param applicationId
	 * @return
	 */
	public int deleteApplication(int applicationId) {
		int row = 0;
		String sql = "DELETE FROM application WHERE id=?";
		try {
			row = DBUtil.executeUpdate(sql, applicationId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return row;
	}
}
